﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ICSharpCode.SharpZipLib.Zip;
using COM.Web;
using ICSharpCode.SharpZipLib.GZip;
using WebApp.Web.Service;
using COM.Data;

namespace WebApp.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return Redirect("/TempFile/Index");
        }

        public ActionResult SetLang(string id)
        {
            var url = this.Request.UrlReferrer;
            HttpCookie langCookie = new HttpCookie("lang");
            langCookie["value"] = id;
            langCookie.Expires = DateTime.Now.AddDays(30);
            this.Response.SetCookie(langCookie);
            return Redirect(url.ToString());
        }

        public ActionResult Share()
        {
            return View();
        }

        public ActionResult IndexAll()
        {
            return View();
        }

        public ActionResult Download()
        {
            var configService = Unity.Instance.GetService<IWebConfigService>();
            var xml = configService.GetAppConfigXml("AppVer", isCache: false);
            ViewBag.AndroidAppVer = xml.Element("AndroidApp").Attribute("NewVer").Value;
            ViewBag.WpfAppVer = xml.Element("WpfApp").Attribute("NewVer").Value;
            return View();
        }

        public ActionResult ResetCache()
        {
            var configService = Unity.Instance.GetService<IWebConfigService>();
            configService.ResetCache();
            return this.Redirect("/");
        }

        public ActionResult Feedback()
        {
            return View();
        }

        public ActionResult IndexPage()
        {
            return View("IndexPage");
        }



        public ActionResult IndexKnockout()
        {
            return View("IndexKnockout");
        }

        public ActionResult Initialize()
        {
            //Unity.Instance.GetService<WebApp.Web.Service.IGameRoomHelperService>().Initialize();
            return View();
        }

        public ActionResult Restart()
        {
            //System.Diagnostics.Process.GetCurrentProcess().Kill();
            return View("Initialize");
        }


        public ActionResult Role()
        {
            return View();
        }

        public ActionResult Help()
        {
            return View();
        }


        public ActionResult DownloadZip()
        {
            var file = System.IO.Path.Combine(Unity.Instance.GetService<IWebAppPathService>().AppRunPath, "..", "DownloadApp", "Android", "Release", "org.siai.androidapp-Signed.apk");
            var s = this.ZipFile(file);
            return File(s, "application/octet-stream", "SiAiApp.zip");
        }

        private byte[] ZipFile(string fileName)
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            using (ZipOutputStream s = new ZipOutputStream(stream))
            {
                s.SetLevel(9); // 压缩级别 0-9
                byte[] buffer = new byte[4096]; //缓冲区大小
                ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(fileName));
                entry.DateTime = DateTime.Now;
                s.PutNextEntry(entry);
                using (var fs = System.IO.File.OpenRead(fileName))
                {
                    int sourceBytes;
                    do
                    {
                        sourceBytes = fs.Read(buffer, 0, buffer.Length);
                        s.Write(buffer, 0, sourceBytes);
                    }
                    while (sourceBytes > 0);
                }
                s.Finish();
                var data = stream.ToArray();
                s.Close();
                return data;
            }
        }


    }
}
